/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import com.lehe.common.utils.StringUtil;
import com.lehe.dao.AdminRoleDao;
import com.lehe.entry.AdminRoleDto;
import com.lehe.entry.AdminUserDto;
import com.lehe.entry.BaseResponse;
import com.lehe.entry.SqlParameterDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 类的功能描述
 *
 * @author 王丹
 * @since 2017-03-07
 */
@Service
@Transactional
public class AdminRoleService extends AbstractService{

    @Autowired
    protected AdminRoleDao adminRoleDao;

    public AdminRoleDto queryById(Long adminRoleId){
        return adminRoleDao.queryById(adminRoleId);
    }

    public List<AdminRoleDto> queryAll(){
        return adminRoleDao.queryAll(new SqlParameterDto());
    }
    
    public AdminRoleDto queryByName(String name){
    	List<AdminRoleDto> result = adminRoleDao.queryAll(new SqlParameterDto());
    	for(AdminRoleDto d : result){
    		if(d.getName().equals(name)){
    			return d;
    		}
    	}
    	return null;
    }
    

    public List<AdminRoleDto> queryAll(SqlParameterDto sqlParameterDto){
        return adminRoleDao.queryAll(sqlParameterDto);
    }

    public Long queryAllCount(SqlParameterDto sqlParameterDto){
        return adminRoleDao.queryAllCount(sqlParameterDto);
    }

    public BaseResponse updateAdminUserById(AdminRoleDto adminRoleDto){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
        if(StringUtil.isBlank(adminRoleDto.getName())){
            baseResponse.setMsg("权限名称不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminRoleDto.getMenuIds())){
            baseResponse.setMsg("菜单赋权不能为空");
            return baseResponse;
        }
        if(null == adminRoleDto.getId()){
            baseResponse.setMsg("菜单ID不能为空");
            return baseResponse;
        }
        adminRoleDao.updateAdminRoleById(adminRoleDto);
        baseResponse.setCode(BaseResponse.SUCCESS);
        baseResponse.setMsg("修改权限成功");
        return baseResponse;
    }

    public BaseResponse insertAdminRole(AdminRoleDto adminRoleDto){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
        if(StringUtil.isBlank(adminRoleDto.getName())){
            baseResponse.setMsg("权限名称不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminRoleDto.getMenuIds())){
            baseResponse.setMsg("权限菜单不能为空");
            return baseResponse;
        }
        adminRoleDao.insertAdminRole(adminRoleDto);
        baseResponse.setCode(BaseResponse.SUCCESS);
        return baseResponse;
    }
}
